package com.paomi.client.base;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.paomi.client.MyApplication;
import com.paomi.client.R;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.RxDataUtils;
import com.paomi.client.view.LoadingDialog;

import java.util.List;

/**
 * Created by LS on 2017/5/25 0025.
 * Recycler适配器，用于加载更多界面刷新和回调，控制LCE切换，Loading-Empty
 */

public abstract class BaseGuideAdapter<T, K extends BaseViewHolder> extends BaseQuickAdapter<T, K> {
    protected static final int LCE_LOADING = 0;
    protected static final int LCE_EMPTY = 1;
    protected static final int LCE_ERROR = 2;

    //    private View mLoadView;
    private View mEmptyView;
    private View mErrorView;

    private boolean isLoadMore;

    private OnLoadMoreListener listener;
    private View lceView;
    private boolean isShowLoadMore = true;

    /**
     * @param layoutResId 条目布局
     * @param data
     */
    public BaseGuideAdapter(@LayoutRes int layoutResId, @Nullable List<T> data) {
        super(layoutResId, data);
        initLCEView();
    }

    public BaseGuideAdapter(@Nullable List<T> data) {
        super(data);
        initLCEView();
    }

    public BaseGuideAdapter(@LayoutRes int layoutResId) {
        super(layoutResId);
        initLCEView();
    }

    private Context getContext() {
        return MyApplication.getAppContext();
    }

    /**
     * loading - empty - error
     *
     * @return
     */
//    public abstract View initLCE();
    public abstract void initEmpty(TextView empty);

    private void initLCEView() {
        lceView = View.inflate(getContext(), R.layout.layout_lce_view, null);
//        mLoadView = lceView.findViewById(R.id.lce_loading);
        mEmptyView = lceView.findViewById(R.id.lce_empty);
        mErrorView = lceView.findViewById(R.id.lce_error);
        initEmpty((TextView) mEmptyView);
        setLCEView(LCE_LOADING);
    }

    /**
     * 此处开始是在布局使用{@link },但是使用Loading的每个界面都要初始化Gif，初始化时间较长，导致界面启动速度明显降低
     * 此处调用{@link LoadingDialog},全局只需要初始化一次
     */
    public void setLCEView(int state) {
//        if(state == LCE_LOADING) LoadingDialog.get().showLoading();
//        else LoadingDialog.get().hideLoading();

        setVisible(mEmptyView, state == LCE_EMPTY);
        setVisible(mErrorView, state == LCE_ERROR);

//        setHeaderAndEmpty(true);
        setEmptyView(lceView);
    }

    private void setVisible(View view, boolean visible) {
        if (view != null)
            view.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * 使用此方法不用手动处理加载完成{@link BaseQuickAdapter#loadMoreComplete()}
     *
     * @param listener
     */
    public void setLoadMoreListener(OnLoadMoreListener listener, RecyclerView recyclerView) {

        this.listener = listener;
        setOnLoadMoreListener(new RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                if (BaseGuideAdapter.this.listener != null) {
                    BaseGuideAdapter.this.listener.onLoadMoreRequested();
                }
            }
        }, recyclerView);
    }

    /** 是否显示LoadMoreEnd布局*/
    public void setShowLoadMoreEnd(boolean isShowLoadMore) {
        this.isShowLoadMore = isShowLoadMore;
    }

    /**
     * 这里又添加了一个监听是为了能够获取加载更多的标记，从而处理数据的刷新和填充及LCE切换
     */
    public interface OnLoadMoreListener {

        /**
         * 加载更多开始的回调
         */
        void onLoadMoreRequested();
    }

    /**
     * RecyclerView设置数据统一入口，处理了加载更多和LCE切换
     * @param datas
     */
    public void setDatas(List<T> datas) {
        if (isLoadMore) {
            if (RxDataUtils.isEmpty(datas)) {
                loadMoreEnd(!isShowLoadMore);
            } else {
                loadMoreComplete();
                addData(datas);
            }
            isLoadMore = false;
        } else {
            if (!RxDataUtils.isEmpty(datas)) {
                setNewData(datas);
                // 如果首次加载数据不够一屏，禁用加载更多
//                disableLoadMoreIfNotFullPage();
                // 如果首次加载条数少于一页的数，则表示没有数据
                if(datas.size() < Constants.PAGE_SIZE)
                    loadMoreEnd(!isShowLoadMore);
            } else {
                setLCEView(LCE_EMPTY);
            }
        }
    }
}
